const User = require('../model/User');
var express = require('express');
// 数据验证模块，
const validator = require('validator');
const Dbset = require('../model/db');
const Games = require('../model/Games');
const Reply = require('../model/Reply');
const Article = require('../model/Article')
const setting = require('../setting');
const saveMessage = require('../common/message');
var router = express.Router();
exports.mei = (req, res, next) => {
    var u_id = req.session.user._id
    console.log(u_id)
    if (req.session.user == null) {
        return res.json({ error: 1, message: '系统错误' })
    }
    User.findById(u_id, (err, user) => {


        Reply.find({ "author": u_id }, (err, reply) => {
            if (err) {
                return res.json({ error: 2, message: "没评价过呢" })
            }
            // console.log(reply)
            // console.log(user.article_counts)
            var arr = []
            var arr1 = []
            user.game_id.forEach(function (id) {
                // console.log(id)
                arr.push(id)
            });
            user.article_counts.forEach(function (article) {
                // console.log(id)
                arr1.push(article)
            });
            // console.log("数组"+arr)
            Games.find({ gameid: arr }, (err, game) => {
                // console.log(game)
                Article.find({ "_id": arr1 }, (err, article) => {
                    // console.log(article)
                    Article.find({ author: u_id }, (err, articles) => {
                        res.json({
                            error: 0, data: {
                                user,
                                reply,
                                game,
                                article,
                                articles
                            }
                        })
                    })
                })
            })
        })
    })
}
exports.mmset = (req, res, next) => {
    // console.log(req.body)
    // console.log(req.body.bron.substring(0, 5))
    if (req.session.user == null) {
        return res.json({ error: 1, message: "请先登录哟" })
    }
    User.findById(req.session.user._id, (err, user) => {
        console.log(user)
        user.name = req.body.name
        user.bron = req.body.bron.substring(0, 4) + "年" + req.body.bron.substring(5, 6) + "月"
        user.isMale = req.body.isMale
        user.phone = req.body.phone
        user.motto = req.body.motto
        user.save()
        res.json({ error: 0, message: "保存成功" })
    })
}
// ==============搜索==============
exports.ggetinfo = (req, res, next) => {
    // console.log(req.body)
    var info = validator.trim(req.body.info)
    // console.log(info)
    var str = RegExp(info, 'i')
    Games.find({ gamename: str }, (err, game) => {
        User.find({ name: str }, (err, user) => {
            // console.log(user)
            Article.find({ title: str }).populate("author").then((article) => {
                console.log(article)
                if (err) {
                    res.json({ error: 1, message: "暂时没有这个内容哟" })
                }
                res.json({
                    error: 0, data: {
                        game,
                        user,
                        article
                    }
                })
            }).catch((err) => {
                console.log(err)
            })
        })

    })
}
exports.scert = (req, res, next) => {
    console.log(req.body.ids)
    User.findById(req.body.ids, function (err, user) {
        if (err) {
            return res.json({ error: 1, message: "头像处的错误" })
        }
        // console.log(user)
        res.json({ error: 0, data: user })
    })
}
exports.deeeel = (req, res, next) => {
    console.log(req.body.ids)
    Article.findByIdAndRemove({ '_id': req.body.ids }, (err) => {
        if (err) {
            return res.json({ error: 1, message: "出错在userset117行" })
        }
        res.json({ error: 0, message: "删除成功" })
    })
}